/*
 * Powered By [rapid-framework]
 * Web Site: http://www.rapid-framework.org.cn
 * Google Code: http://code.google.com/p/rapid-framework/
 * Since 2008 - 2012
 */

package com.tech15.task.action;

import static javacommon.util.extjs.Struts2JsonHelper.outXmlJson;

import java.util.Map;

import javacommon.base.BaseStruts3Action;
import javacommon.util.ResInfo;

import org.apache.struts2.ServletActionContext;

import cn.org.rapid_framework.page.Page;
import cn.org.rapid_framework.util.ObjectUtils;

import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;
import com.tech15.pub.service.PubCcsqManager;
import com.tech15.task.model.Task;
import com.tech15.task.model.TaskProcess;
import com.tech15.task.service.TaskManager;
import com.tech15.task.service.TaskProcessManager;
import com.tech15.task.service.TaskReturnManager;
import com.tech15.task.service.ZdxzdcManager;
import com.tech15.task.vo.query.TaskQuery;

/**
 * @author 胡作正 email:zuozhenghu@sina.com
 * @version 1.0
 * @since 1.0
 */


public class TaskAction extends BaseStruts3Action implements Preparable,ModelDriven{
	//默认多列排序,example: username desc,createTime asc
	protected static final String DEFAULT_SORT_COLUMNS = "creattime desc"; 
	
	//forward paths
	protected static final String MY_TASK= "/task/my_task.jsp";
	protected static final String MY_TASK_SEARCH = "/task/my_search.jsp";
	protected static final String LIST_JSP= "/task/task_list.jsp";
	protected static final String SEARCH_JSP= "/task/task_search.jsp";
	protected static final String CREATE_JSP = "/task/task_add.jsp";
	protected static final String EDIT_JSP = "/task/task_edit.jsp";
	protected static final String SHOW_JSP = "/task/task_show.jsp";
	protected static final String PRINT_JSP = "/task/task_print.jsp";
	//redirect paths,startWith: !
	protected static final String LIST_ACTION = "!/task/list.do";
	private static final String TASK_PROCESS = "/task/task_process.jsp";
	
	private TaskManager taskManager;
	private TaskProcessManager taskProcessManager;
	private ZdxzdcManager zdxzdcManager;
	private TaskReturnManager taskReturnManager;
	private PubCcsqManager pubCcsqManager;
	
	private Task task;
	Long id = null;
	private String[] items;

	public void prepare() throws Exception {
		if (isNullOrEmptyString(id)) {
			task = new Task();
		} else {
			task = (Task)taskManager.getById(id);
		}
	}
	
	public void setTaskReturnManager(TaskReturnManager manager) {
		this.taskReturnManager = manager;
	}
	
	public void setTaskProcessManager(TaskProcessManager manager) {
		this.taskProcessManager = manager;
	}
	
	public void setZdxzdcManager(ZdxzdcManager manager) {
		this.zdxzdcManager = manager;
	}
	
	public void setTaskManager(TaskManager manager) {
		this.taskManager = manager;
	}	
	public void setPubCcsqManager(PubCcsqManager manager) {
		this.pubCcsqManager = manager;
	}
	
	public Object getModel() {
		return task;
	}
	
	public void setTaskid(Long val) {
		this.id = val;
	}

	public void setItems(String[] items) {
		this.items = items;
	}
	
	/**查询我的任务*/
	public String myTask(){
		TaskQuery query = newQuery(TaskQuery.class,DEFAULT_SORT_COLUMNS);
		query.setHanlder(getLoginInfo().getUserid());
		query.setHanldestate(0);
		Page page = taskManager.myTask(query);
		savePage(page,query);
		return MY_TASK;
	}
	/**查询我的任务*/
	public String mySearch(){
		TaskQuery query = newQuery(TaskQuery.class,DEFAULT_SORT_COLUMNS);
		query.setHanlder(getLoginInfo().getUserid());
		query.setHanldestate(1);
		Page page = taskManager.mySearch(query);
		savePage(page,query);
		return MY_TASK_SEARCH;
	}
	
	/**转下一审批人*/
	public void zxyspTask(){
		Map<String, Object> resInfo = null;
		try{
			task.setOpttype(1);
			String realpath = ServletActionContext.getServletContext().getRealPath(UPLOADPATH);
			taskManager.zxyspTask(task,realpath,getAttachFiles(),getAttachFilesFileName(),getAttachFilesContentType());
			resInfo = ResInfo.getResInfo("",false, false, 2, this.OPERATE_SUCCESS,"");
		}catch(Exception e){
			e.printStackTrace();
			resInfo = ResInfo.getResInfo("",false, false, 1, this.OPERATE_FAILED,"");
		}
		outXmlJson(resInfo);
	}
	
	/**审批完成*/
	public void spwcTask(){
		Map<String, Object> resInfo = null;
		try{
			task.setOpttype(2);
			String realpath = ServletActionContext.getServletContext().getRealPath(UPLOADPATH);
			Integer mf = taskManager.spwcTask(task,realpath,getAttachFiles(),getAttachFilesFileName(),getAttachFilesContentType());
			if(mf == 1){
				resInfo = ResInfo.getResInfo("",false, false, 2, this.OPERATE_SUCCESS,"");
			}else if(mf == 0){
				resInfo = ResInfo.getResInfo("",false, false, 1, "主送部门文书未设置，请联系管理员！","");
			}else if(mf == 2){
				resInfo = ResInfo.getResInfo("",false, false, 1, "主送部门文书未设置，请联系管理员！","");
			}
		}catch(Exception e){
			e.printStackTrace();
			resInfo = ResInfo.getResInfo("",false, false, 1, this.OPERATE_FAILED,"");
		}
		outXmlJson(resInfo);
	}
	
	/**审核任务*/
	public void tjshTask(){
		Map<String, Object> resInfo = null;
		try{
			task.setOpttype(2);
			String realpath = ServletActionContext.getServletContext().getRealPath(UPLOADPATH);
			taskManager.tjshTask(task,realpath,getAttachFiles(),getAttachFilesFileName(),getAttachFilesContentType());
			resInfo = ResInfo.getResInfo("",false, false, 2, this.OPERATE_SUCCESS,"");
		}catch(Exception e){
			e.printStackTrace();
			resInfo = ResInfo.getResInfo("",false, false, 1, this.OPERATE_FAILED,"");
		}
		outXmlJson(resInfo);
	}
	
	/**退回任务提交审批任务*/
	public void tjspTask(){
		Map<String, Object> resInfo = null;
		try{
			task.setOpttype(1);
			String realpath = ServletActionContext.getServletContext().getRealPath(UPLOADPATH);
			taskManager.tjspTask(task,realpath,getAttachFiles(),getAttachFilesFileName(),getAttachFilesContentType());
			resInfo = ResInfo.getResInfo("",false, false, 2, this.OPERATE_SUCCESS,"");
		}catch(Exception e){
			e.printStackTrace();
			resInfo = ResInfo.getResInfo("",false, false, 1, this.OPERATE_FAILED,"");
		}
		outXmlJson(resInfo);
	}
	
	/**审核任务*/
	public void shTask(){
		Map<String, Object> resInfo = null;
		try{
			task.setOpttype(2);
			String realpath = ServletActionContext.getServletContext().getRealPath(UPLOADPATH);
			taskManager.shTask(task,realpath,getAttachFiles(),getAttachFilesFileName(),getAttachFilesContentType());
			resInfo = ResInfo.getResInfo("",false, false, 2, this.OPERATE_SUCCESS,"");
		}catch(Exception e){
			e.printStackTrace();
			resInfo = ResInfo.getResInfo("",false, false, 1, this.OPERATE_FAILED,"");
		}
		outXmlJson(resInfo);
	}
	
	/**执行分配任务*/
	public void zxfpTask(){
		Map<String, Object> resInfo = null;
		try{
			task.setOpttype(2);
			String realpath = ServletActionContext.getServletContext().getRealPath(UPLOADPATH);
			taskManager.zxfpTask(task,realpath,getAttachFiles(),getAttachFilesFileName(),getAttachFilesContentType());
			resInfo = ResInfo.getResInfo("",false, false, 2, this.OPERATE_SUCCESS,"");
		}catch(Exception e){
			e.printStackTrace();
			resInfo = ResInfo.getResInfo("",false, false, 1, this.OPERATE_FAILED,"");
		}
		outXmlJson(resInfo);
	}
	
	/**执行转他人任务*/
	public void zxztrTask(){
		Map<String, Object> resInfo = null;
		try{
			task.setOpttype(2);
			String realpath = ServletActionContext.getServletContext().getRealPath(UPLOADPATH);
			taskManager.zxztrTask(task,realpath,getAttachFiles(),getAttachFilesFileName(),getAttachFilesContentType());
			resInfo = ResInfo.getResInfo("",false, false, 2, this.OPERATE_SUCCESS,"");
		}catch(Exception e){
			e.printStackTrace();
			resInfo = ResInfo.getResInfo("",false, false, 1, this.OPERATE_FAILED,"");
		}
		outXmlJson(resInfo);
	}
	
	/**执行处理任务*/
	public void zxclTask(){
		Map<String, Object> resInfo = null;
		try{
			task.setOpttype(2);
			String realpath = ServletActionContext.getServletContext().getRealPath(UPLOADPATH);
			taskManager.zxclTask(task,realpath,getAttachFiles(),getAttachFilesFileName(),getAttachFilesContentType());
			resInfo = ResInfo.getResInfo("",false, false, 2, this.OPERATE_SUCCESS,"");
		}catch(Exception e){
			e.printStackTrace();
			resInfo = ResInfo.getResInfo("",false, false, 1, this.OPERATE_FAILED,"");
		}
		outXmlJson(resInfo);
	}
	
	/**执行提交任务（抄送处理）*/
	public void tjrwTask(){
		Map<String, Object> resInfo = null;
		try{
			task.setOpttype(2);
			String realpath = ServletActionContext.getServletContext().getRealPath(UPLOADPATH);
			taskManager.tjrwTask(task,realpath,getAttachFiles(),getAttachFilesFileName(),getAttachFilesContentType());
			resInfo = ResInfo.getResInfo("",false, false, 2, this.OPERATE_SUCCESS,"");
		}catch(Exception e){
			e.printStackTrace();
			resInfo = ResInfo.getResInfo("",false, false, 1, this.OPERATE_FAILED,"");
		}
		outXmlJson(resInfo);
	}
	
	/**执行处理任务*/
	public void closeTask(){
		Map<String, Object> resInfo = null;
		try{
			task.setOpttype(2);
			String realpath = ServletActionContext.getServletContext().getRealPath(UPLOADPATH);
			Integer m = taskManager.closeTask(task,realpath,getAttachFiles(),getAttachFilesFileName(),getAttachFilesContentType());
			if(m == 1){
				resInfo = ResInfo.getResInfo("",false, false, 2, this.OPERATE_SUCCESS,"");
			}else if(m == 0){
				resInfo = ResInfo.getResInfo("",false, false, 1, "任务执行未完成！","");
			}else if(m == 2){
				resInfo = ResInfo.getResInfo("",false, false, 1, "抄送部门没有接收人，请联系管理员处理！","");
			}else if(m == 3){
				resInfo = ResInfo.getResInfo("",false, false, 1, "部分抄送部门没有接收人，请联系管理员处理！","");
			}
		}catch(Exception e){
			e.printStackTrace();
			resInfo = ResInfo.getResInfo("",false, false, 1, this.OPERATE_FAILED,"");
		}
		outXmlJson(resInfo);
	}
	
	/**处理任务*/
	public void thTask(){
		Map<String, Object> resInfo = null;
		try{
			taskManager.thTask(task);
			resInfo = ResInfo.getResInfo("",false, false, 2, this.OPERATE_SUCCESS,"");
		}catch(Exception e){
			e.printStackTrace();
			resInfo = ResInfo.getResInfo("",false, false, 1, this.OPERATE_FAILED,"");
		}
		outXmlJson(resInfo);
	}
	
	public void taskCount(){
		Integer taskCount = taskManager.getWclTask(getLoginInfo().getUserid());
		Integer xzdcCount = zdxzdcManager.myTaskCount(getLoginInfo().getUserid());
		Integer taskReturnCount = taskReturnManager.myReturnCount(getLoginInfo().getUserid());
		Integer ccsqCount = pubCcsqManager.getTaskCount(getLoginInfo().getUserid());
		
		Integer count = taskCount + xzdcCount+taskReturnCount+ccsqCount;
		getRequest().setAttribute("myCount", count);
		getRequest().setAttribute("taskCount", taskCount);
		getRequest().setAttribute("xzdcCount", xzdcCount);
		getRequest().setAttribute("taskReturnCount", taskReturnCount);
		getRequest().setAttribute("ccsqCount", ccsqCount);
	}
	
	/** 执行搜索 */
	public String list() {
		TaskQuery query = newQuery(TaskQuery.class,DEFAULT_SORT_COLUMNS);
		
		Page page = taskManager.findPage(query);
		savePage(page,query);
		return LIST_JSP;
	}
	
	/** 执行搜索 */
	public String search() {
		TaskQuery query = newQuery(TaskQuery.class,DEFAULT_SORT_COLUMNS);
		if("processing".equals(opt)){
			query.setTaskzt(1);
		}else if("complete".equals(opt)){
			query.setTaskwczt(1);
		}
		if(getLoginInfo().getCategory() == 2){
			query.setDeptid(getLoginInfo().getDeptid());
		}
		Page page = taskManager.findPage(query);
		savePage(page,query);
		return SEARCH_JSP;
	}
	
	/** 查看对象*/
	public String show() {
		return SHOW_JSP;
	}
	
	public String print() {
		return PRINT_JSP;
	}
	
	/** 进入新增页面*/
	public String create() {
		return CREATE_JSP;
	}
	
	/** 保存新增对象 */
	public void save() {
		Map<String, Object> resInfo = null;
		try{
			task.setOpttype(1);
			task.setCreater(this.getLoginInfo().getUserid());
			String realpath = ServletActionContext.getServletContext().getRealPath(UPLOADPATH);
			if(ObjectUtils.isEmpty(task.getHanlder()) && ObjectUtils.isEmpty(task.getXmspr())){
				resInfo = ResInfo.getResInfo("",false, false, 1, "审批人不能为空","");
			}else{
				taskManager.saveTask(task,realpath,getAttachFiles(),getAttachFilesFileName(),getAttachFilesContentType());
				resInfo = ResInfo.getResInfo("",false, false, 2, this.OPERATE_SUCCESS,"");
			}
		}catch(Exception e){
			e.printStackTrace();
			resInfo = ResInfo.getResInfo("",false, false, 1, this.OPERATE_FAILED,"");
		}
		outXmlJson(resInfo);
	}
	
	/**进入更新页面*/
	public String getTaskProcess() {
		TaskProcess tp = taskProcessManager.getById(task.getTpid());
		this.task.setTp(tp);
		return TASK_PROCESS;
	}
	
	/**进入更新页面*/
	public String edit() {
		return EDIT_JSP;
	}
	
	/**保存更新对象*/
	public void update() {
		Map<String, Object> resInfo = null;
		try{
			taskManager.update(this.task);
			resInfo = ResInfo.getResInfo("",false, false, 2, this.OPERATE_SUCCESS,"");
		}catch(Exception e){
			e.printStackTrace();
			resInfo = ResInfo.getResInfo("",false, false, 1, this.OPERATE_FAILED,"");
		}
		outXmlJson(resInfo);
	}
	
	/**删除对象*/
	public void delete() {
		Map<String, Object> resInfo = null;
		try {
			taskManager.removeById(id);
			resInfo = ResInfo.getResInfo("",false, false, 2, this.OPERATE_SUCCESS,"");
		} catch (Exception e) {
			e.printStackTrace();
			resInfo = ResInfo.getResInfo("",false, false, 1,this.OPERATE_FAILED,"");
		}		
		outXmlJson(resInfo);
	}
	
	
	private String opt;

	public String getOpt() {
		return opt;
	}

	public void setOpt(String opt) {
		this.opt = opt;
	}
	
}
